<?php
class BannerAction extends AdministratorAction {
	public function _initialize(){
		$this->modelName = 'Banner';
		parent::_initialize();
	}
	
	public function index(){
		$banners = $this->modelObj->select();
		$this->assign('banners',$banners);
		$this->display();
	}
	
	public function add(){
		if ( $this->isPost() ){
			$data = upload();
		}
		
		if ( $data['error']==0 ){
			$banner = array(
				'name'=>$data['name'],
				'path'=>$data['url'],
				'thumb'=>$data['thumb'],
				'link'=>''
			);
			$this->modelObj->add($banner);			
		}
		else{
			$this->error('上传图片失败');
		}

		$this->ajaxReturn($data);
	}
	
	public function delete(){
		$bid = $this->_get('id');
		$where = array('id'=>$bid);
		$banner = $this->modelObj->where($where)->find();
		if ( !$bid || !$this->modelObj->where($where)->find() ){
			$this->error('您指定的banner不存在或已被删除');
		}
		else{
			if ( file_exists($banner['path']) ){
				unlink($banner['path']);
			}
			if ( file_exists($banner['thumb'])){
				unlink($banner['thumb']);
			}
			$status = $this->modelObj->where($where)->delete();
		}
		$this->success('删除成功');
	}
	

}